<?php
/**
 * Created by PhpStorm.
 * User: caichunmin
 * 推荐接口
 * Date: 2017/1/9
 * Time: 17:37
 */

namespace SDXApi\Controller;

use Think\Controller;
use SDXApi\Service\RecommendService;

class RecommendController extends Controller
{
    protected $aliyun_url = 'https://shujuapi.aliyun.com/dataplus_129090/re/';
    protected function _initialize()
    {
        header("Access-Control-Allow-Origin: *");
    }

    //推荐结果列表
    public function lists(){

        $wid = I('post.wid',8, 'int');
        $user_id = I('post.uid',0, 'int');
        $goods_id = I('post.goods_id',0, 'int');
        $num = I('post.num',6, 'int');
        $page = I('post.page',1, 'int');
        $plate = I('post.plate',1, 'int'); //板块位置

        if(!$wid || !$num){
            returnApiError('参数错误');
        }
        if(!in_array($plate, array(1,2,3,4,5,6,7,8))){
            $plate = 1;
        }

        $page = $page>13?13:$page;
        $num = $num>150?150:$num;
        $page_type = 5;

        $recommend = new RecommendService();

        //商品详情推荐
        if($goods_id){
            $code_name = 'recommend';
        }else{
            $code_name = $recommend->get_code_name($plate);
            $page_type = $plate+$page_type;
        }

        $return_data = $recommend->get_list($code_name, $user_id, $goods_id, $num, $wid, $page, $plate);

        //添加到用户行为
        if($user_id){
            $param = array('isertList', $user_id, 'view', $return_data['goods_list'], $wid, $page_type, '', $goods_id);
            \Think\Hook::listen('receng', $param);
        }

        if($return_data['goods_list'])
            returnApiSuccess($return_data);
        else
            returnApiError('无数据');
    }



    //启动预数据预处理
    public function data_dispose(){
        $time_list = array(8,14);
        if(!in_array(date('H'), $time_list)){
            exit;
        }

        $url = $this->aliyun_url ."etl";
        $content = array(
            'biz_code'=>'sdx_code',
            'ds'=> date('Ymd', strtotime("-1 day"))
        );
        //获取推荐引擎返回数据
        $data = get_recommend_shop($url, $content);
        print_r($data);exit;

    }

    //查询任务运行结果
    public function task_status(){
        $task_id = I('get.task_id');
        $url = $this->aliyun_url ."status?task_id=$task_id";
        $data = get_recommend_shop($url);
        print_r($data);exit;
    }

    //启动离线任务
    public function start_task(){
        $time_list = array(9,15);
        if(!in_array(date('H'), $time_list)){
            exit;
        }
        $url = $this->aliyun_url ."tasks";
        $content = array(
            'biz_code'=>'sdx_code',
            'ds'=> date('Ymd', strtotime("-1 day"))
        );
        //获取推荐引擎返回数据
        $data = get_recommend_shop($url, $content);
        print_r($data);exit;
    }

    /*
     * 获取阿里云推荐url
     */
    protected function get_aliyun_url($user_id, $goods_id=0){
        if($goods_id)
            //return $this->aliyun_url ."doRec?biz_code=sdx_code&scn_code=page&user_id=$user_id&item_id=$goods_id";
            return $this->aliyun_url ."doRec?biz_code=sdx_code&scn_code=large_scale&user_id=$user_id&item_id=$goods_id";
        else
            return $this->aliyun_url ."doRec?biz_code=sdx_code&scn_code=home&user_id=$user_id";
    }

    public function get_behavior_list(){
        $list = M('user_behavior')->order('id desc')->select();
        print_r($list);exit;
    }

    public function get_behavior_count(){
        $list = M('user_behavior')->count();
        print_r($list);exit;
    }


}